<?php
set_time_limit(300);
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
//定义在单独操作内容的时候，同时更新相关栏目页面
define('RELATION_HTML',true);

pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form','',0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format','',0);

/**
 * 导师管理
 */
class moto extends admin
{
	private $db,$moto;
	public $siteid;

	public function __construct()
	{
		parent::__construct();
		$this->db = pc_base::load_model('ss_member_model');
		$this->moto = pc_base::load_model('moto_member_model');
		$this->siteid = get_siteid();
	}

	/**
	 * 模特列表
	 */
	public function index ()
	{
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = '';
		if ($_GET['username']) {
			$username = $_GET['username'];
			$where .= " `username` LIKE '%$username%' ";		
		}
		$moto_list = $this->db->listinfo($where, 'id DESC', $page, 15);
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('moto_list');
	}

	/**
	 * 添加模特扩展属性
	 */
	public function add ()
	{
		$motoid = $_GET['motoid']?(int)$_GET['motoid']:'';
		if (!$motoid) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$moto = $this->check($_POST['moto']);
			$moto['motoid'] = $motoid;
			$id = $this->moto->insert($moto, true);
			if ($id) {
				showmessage(L('add_moto_success'), HTTP_REFERER);
			} else {
				showmessage(L('add_moto_error'), HTTP_REFERER);
			}	
		} else {
			//获取站点模板信息
			$show_dialog = $show_validator = true;
			$info = $this->moto->get_one(array('motoid'=>$motoid, 'siteid'=>$this->siteid));
			extract($info);
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('moto_add');
		}
	}

	/**
	 * 模特扩展属性修改
	 */
	public function edit (){
		if (!isset($_GET['id']) || empty($_GET['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = intval($_GET['id']);
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$moto = $this->check($_POST['moto'], 'edit');
			$id = $this->moto->update($moto, array('id'=>$id, 'siteid'=>$this->siteid));
			if ($id) {
				showmessage(L('edit_moto_success'), HTTP_REFERER);
			} else {
				showmessage(L('edit_moto_error'), HTTP_REFERER);
			}	
		}
	}

	/**
	 * 模特扩展属性删除
	 */
	public function delete ()
	{
		if(!isset($_GET['id']) || empty($_GET['id'])) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			$id = intval($_GET['id']);
			if($id < 1) return false;
			$result = $this->moto->delete(array('id'=>$id));
			if($result){
				showmessage(L('operation_success'),HTTP_REFERER);
			}else {
				showmessage(L("operation_failure"),HTTP_REFERER);
			}
		}
	}

	/**
	 * 表单验证
	 * @param array $data 表单传递的值
	 * @param string $a add/edit添加操作时，自动加上默认值
	 */
	private function check($data, $a = 'add') {
		if($a=='add') {
			$data['siteid'] = $this->siteid;
			$data['addtime'] = SYS_TIME;
			$data['addusername'] = param::get_cookie('admin_username');
			$data['adduserid'] = $_SESSION['userid'];
		} else {
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}
	
}